﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchView.GameContent.Units;
using TorchView.GameContent.Data;
using System.IO;

namespace TorchView.SharedStashEditor.Models
{
    public class ItemCollection
    {
        private static ItemCollection _defaultCollection;
        public static ItemCollection DefaultCollection
        {
            get
            {
                if (_defaultCollection == null)
                {
                    _defaultCollection = new ItemCollection();
                    _defaultCollection.Load();
                }
                return _defaultCollection;
            }
        }

        private SortedDictionary<string, SortedSet<string>> _itemCat;
        private SortedDictionary<string, Item> _itemList;
        private SortedDictionary<long, Item> _itemListById;
        private bool loaded = false;

        public ItemCollection()
        {
            _itemCat = new SortedDictionary<string, SortedSet<string>>();
            _itemList = new SortedDictionary<string, Item>();
            _itemListById = new SortedDictionary<long, Item>();
        }

        public void Load()
        {
            if (loaded) return;

            foreach (var filename in PakReader.GetFiles("media/units/items", "*.dat.adm", SearchOption.AllDirectories))
            {
                Item item = new Item();

                Stream stream = PakReader.OpenFile(filename);
                DataBlock block = AdmSerializer.Load(stream);
                item.LoadFromDataBlock(block);

                string name = item.Name;
                string type = item.UnitType;

                if (!item.DontCreate)
                {
                    //Console.WriteLine(String.Format("[{0}] {1}",type,name));
                    SortedSet<string> itemSet;
                    if (!_itemCat.TryGetValue(type, out itemSet))
                    {
                        itemSet = new SortedSet<string>();
                        _itemCat.Add(type, itemSet);
                    }

                    if (!_itemList.ContainsKey(name))
                    {
                        itemSet.Add(name);
                        _itemList.Add(name, item);

                        if (!_itemListById.ContainsKey(item.UnitGuid))
                        {
                            _itemListById.Add(item.UnitGuid, item);
                        }
                    }
                }
            }

            loaded = true;
        }

        public Item this[string name]
        {
            get
            {
                Item item;
                if (_itemList.TryGetValue(name, out item))
                    return item;
                else
                    return null;
            }
        }

        public Item this[long id]
        {
            get
            {
                Item item;
                if (_itemListById.TryGetValue(id, out item))
                    return item;
                else
                    return null;
            }
        }

        public IEnumerable<string> Items
        {
            get { return _itemList.Keys; }
        }

        public IEnumerable<string> Categories
        {
            get { return _itemCat.Keys; }
        }

        public IEnumerable<string> getCategory(string type)
        {
            SortedSet<string> itemSet;
            if (_itemCat.TryGetValue(type, out itemSet))
                return itemSet;
            else
                return null;
        }
    }
}
